package com.egg.ylt.presenter.impl;


import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;

import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.common.utils.MoneyFormatUtil;
import com.yonyou.framework.library.common.utils.StringUtil;
import com.egg.ylt.presenter.IAddGrowRecordPresenter;
import com.egg.ylt.view.IAddGrowRecordView;
import com.google.zxing.common.StringUtils;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.text.DecimalFormat;
import java.util.HashMap;

/**
 * Created by lijiateng on 2019/1/19.
 */

public class AddGrowRecordPresenterImpl extends BasePresenter<IAddGrowRecordView> implements IAddGrowRecordPresenter {

    public void addOrUpdateHeightAndWeight(String height, String weight, String date, String babyId, String id) {

        if (!verifyUserInput(false, weight, height)) {
            return;
        }

        if (!isInputLegal(weight, height)) {
            return;
        }

        mView.showDialogLoading("正在加载中...");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("height", com.egg.ylt.Utils.StringUtil.get(height));
        hashMap.put("weight", com.egg.ylt.Utils.StringUtil.get(weight));
        hashMap.put("date", com.egg.ylt.Utils.StringUtil.get(date));
        hashMap.put("babyId", com.egg.ylt.Utils.StringUtil.get(babyId));
        hashMap.put("id", com.egg.ylt.Utils.StringUtil.get(id));
        RequestManager.getInstance().requestPostByAsyn(API.POST_UPDATE_HEIGHT_WEIGHT, hashMap, new ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.modifySucceed();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                mView.showBusinessError(error);
            }
        });

    }

    private boolean isInputLegal(String weight, String height) {
        if (TextUtils.isEmpty(getFormattedInput(weight))
                && TextUtils.isEmpty(getFormattedInput(height))) {
            mView.showError("您还没有填写身高和体重哦！");
            return false;
        }
        return true;
    }

    /**
     * 校验用户输入
     *
     * @param height
     * @param weight
     * @return
     */
    public boolean verifyUserInput(boolean isBack, String weight, String height) {

        if (TextUtils.isEmpty(height) && TextUtils.isEmpty(weight)) {
            if (!isBack) {
                mView.showError("您还没有填写身高和体重哦！");
            }
            return false;
        }

        return true;
    }


    public String getFormattedInput(String targetStr) {

        if (TextUtils.isEmpty(targetStr) || "0.".equals(targetStr)) {
            return "";
        }

        // 不足两位小数时，自动补全
        targetStr = new DecimalFormat("0.00").format(Double.valueOf(targetStr));

        // 删除多余的0
        int num = targetStr.length();
        int zeroIndex = -1;
        for (int i = 0; i < num; i++) {
            char c = targetStr.charAt(i);
            if (c != '0') {
                zeroIndex = i;
                break;
            } else if (i == num - 4) {
                // 因为已经自动补全两位小数，所以数字最小长度即为4位：0.00
                // 所以如果0出现的位置为第0个位置时，则将0标记为zeroIndex
                zeroIndex = i;
                break;
            }
        }
        if (zeroIndex != -1) {
            targetStr = targetStr.substring(zeroIndex);
        }

        // 屏蔽输入金额为0、或者只输入 . 的情况
        if (targetStr.equals("0.00") || "0.".equals(targetStr)) {
            return "";
        }

        return targetStr;
    }

}
